<link rel="stylesheet" href="./nepadmin/css/formSelects.css" media="all">
<style>
    .xm-select-dl{
        top:unset !important;
    }
</style>

<script id="edit_user" type="text/html">
    <form class="layui-form" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">昵称</label>
            <div class="layui-input-block">
                <input type="text" name="u_name" required  lay-verify="required" placeholder="请输入用户名" value="{{ d.u_name }}" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">账号</label>
            <div class="layui-input-block">
                <input type="text" name="u_account" required  lay-verify="required" placeholder="请输入账号" value="{{ d.u_account }}" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-block">
                <input type="password" name="u_password"  placeholder="请输入密码" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">角色</label>
            <div class="layui-input-block">
                <select name="role_id" xm-select="u_role" xm-select-search="" xm-select-search-type="dl" lay-verify="required" >
                    {{#  layui.each(d.roles, function(index, item){ }}
                    <option value="{{ item.role_id }}"
                    {{#  if(d.role_id == item.role_id){ }}
                    selected
                    {{#  } }}
                    >{{ item.role_name }}</option>
                    {{#  }); }}
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">单选框</label>
            <div class="layui-input-block">
                <input type="radio" name="u_status" value=1 title="正常"
                       {{#  if(d.u_status == 1){ }}
                       checked
                       {{#  } }}
                >
                <input type="radio" name="u_status" value=0 title="禁用"
                       {{#  if(d.u_status == 0){ }}
                       checked
                       {{#  } }}
                >
            </div>
        </div>
        <input type="hidden" name="u_id" value="{{ d.u_id }}">
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="edit-user">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</script>
<div id="edit_user_bd"></div>
<script>
    layui.use(['admin', 'table', 'form', 'dropdown', 'jquery', 'formSelects','laytpl'], function (admin, table, form, dropdown, $){
        var $ = layui.jquery;
        var laytpl = layui.laytpl;
        var u_id = POPUP_DATA.u_id;
        var roleName = [];
        var authList = [];
        var formSelects = layui.formSelects;

        admin.get({
            api: 'getOneUser',
            data:{'u_id':u_id},
            success:function (res) {
                let user = res.result;
                let tpl = $("#edit_user").html();
                admin.post({
                    api: 'getAllRole',
                    success:function (data) {

                        if (!data.result.list){ layer.alert('获取角色列表失败'); }
                        user.roles = data.result.list;
                        laytpl(tpl).render(user, function (html) {
                            $("#edit_user_bd").html(html);
                        });
                        form.render();
                    }
                });
            }
        });

        // 监听添加用户
        form.on('submit(edit-user)', function (data) {
            admin.post({
                api: 'editUser',
                data: data.field,
                success:function (res) {
                    layer.msg('修改成功', {
                        icon:1,
                        success :function (index, layero) {
                            setTimeout(function(){
                                layer.closeAll();
                                layui.view.tab.refresh();
                            },800);
                        }
                    });
                }
            });
            return false;
        });

    })
</script>